package com.ruoyi.system.service.impl;

import java.util.*;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.domain.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.BieRecordsMapper;
import com.ruoyi.system.service.IBieRecordsService;

/**
 * 访问记录Service业务层处理
 * 
 * @author zxl
 * @date 2021-05-03
 */
@Service
public class BieRecordsServiceImpl implements IBieRecordsService 
{
    @Autowired
    private BieRecordsMapper bieRecordsMapper;

    @Autowired
    private BieHouseholderServiceImpl householderService;

    @Autowired
    private BieHouseServiceImpl houseService;

    @Autowired
    private BieAccessCardServiceImpl bieAccessCardService;

    @Autowired
    private BieBlacklistServiceImpl bieBlacklistService;


    @Autowired
    private BieAlarmRecordsServiceImpl bieAlarmRecordsService;

    /**
     * 任务调度
     * 凌晨12点将没出门的全部出门处理，并且警报处理
     */

    //@Autowired
    @Override
     public void autoHandlerComeOut(){


//        Stack<String> stack=new Stack<>();
//        List<BieRecords> list = this.selectTodayBieRecordsList();
//        list.forEach(item->{
//
//            if(stack.search(item.getAccessCardId())>0){
//                //存在,入栈
//                stack.push(item.getAccessCardId());
//
//            }else{
//                //不存在
//                stack.pop();
//            }
//        });
        System.out.println("定时任务执行了");

        List<String> ids = bieRecordsMapper.selectAllUnOut();
        System.out.println(ids);
        for (String id : ids) {
            BieRecords records = new BieRecords();
            records.setRecordsType(0L);
            records.setRecordsId(UUID.randomUUID().toString());
            records.setAccessCardId(id);
            BieAccessCard card = bieAccessCardService.selectBieAccessCardById(id);
            BeanUtils.copyProperties(card,records);
            records.setCreateTime(new Date());
            //强制刷出
            records.setRemarks("系统强制刷出");
            bieRecordsMapper.insertBieRecords(records);
            //进入警报表
            BieAlarmRecords bieAlarmRecords = new BieAlarmRecords();
            bieAlarmRecords.setCrateTime(new Date());
            bieAlarmRecords.setRecordsId(UUID.randomUUID().toString());
            bieAlarmRecords.setAccessId(id);
            bieAlarmRecordsService.insertBieAlarmRecords(bieAlarmRecords);
        }

    }

    /**
     * 查询访问记录
     * 
     * @param recordsId 访问记录ID
     * @return 访问记录
     */
    @Override
    public BieRecords selectBieRecordsById(String recordsId)
    {
        return bieRecordsMapper.selectBieRecordsById(recordsId);
    }

    /**
     * 查询访问记录列表
     * 
     * @param bieRecords 访问记录
     * @return 访问记录
     */
    @Override
    public List<BieRecords> selectBieRecordsList(BieRecords bieRecords)
    {
        return bieRecordsMapper.selectBieRecordsList(bieRecords);
    }

    @Override
    public List<BieRecords> selectTodayBieRecordsList() {
        return bieRecordsMapper.selectTodayBieRecordsList();
    }

    /**
     * 新增访问记录
     * 
     * @param bieRecords 访问记录
     * @return 结果
     */
    @Override
    public int insertBieRecords(BieRecords bieRecords)
    {

        //判断是否在黑名单里
        BieBlacklist blacklist = new BieBlacklist();
        blacklist.setAccessCardId(bieRecords.getAccessCardId());
        List<BieBlacklist> list = bieBlacklistService.selectBieBlacklistList(blacklist);
        if(list.size()>0){
            //警报
            BieAlarmRecords bieAlarmRecords = new BieAlarmRecords();
            bieAlarmRecords.setCrateTime(new Date());
            bieAlarmRecords.setRecordsId(UUID.randomUUID().toString());
            bieAlarmRecords.setAccessId(bieRecords.getAccessCardId());
            bieAlarmRecordsService.insertBieAlarmRecords(bieAlarmRecords);
            return -1;
        }else{
            //获取门禁卡表信息
            BieAccessCard bieAccessCard = bieAccessCardService.selectBieAccessCardById(bieRecords.getAccessCardId());


            //住户信息
            String holderId = bieAccessCard.getHolderId();
            BieHouseholder householder = householderService.selectBieHouseholderById(holderId);
            BeanUtils.copyProperties(householder,bieRecords);
            //房子信息
            String houseId = bieAccessCard.getHouseId();
            BieHouse bieHouse = houseService.selectBieHouseById(houseId);
            BeanUtils.copyProperties(bieHouse,bieRecords);

            BeanUtils.copyProperties(bieAccessCard,bieRecords);
            //System.out.println(bieRecords);
            //设置基础信息
            bieRecords.setRecordsId(UUID.randomUUID().toString());
            bieRecords.setCreateTime(new Date());


            return bieRecordsMapper.insertBieRecords(bieRecords);
        }

    }

    /**
     * 修改访问记录
     * 
     * @param bieRecords 访问记录
     * @return 结果
     */
    @Override
    public int updateBieRecords(BieRecords bieRecords)
    {
        return bieRecordsMapper.updateBieRecords(bieRecords);
    }

    /**
     * 批量删除访问记录
     * 
     * @param recordsIds 需要删除的访问记录ID
     * @return 结果
     */
    @Override
    public int deleteBieRecordsByIds(String[] recordsIds)
    {
        return bieRecordsMapper.deleteBieRecordsByIds(recordsIds);
    }

    /**
     * 删除访问记录信息
     * 
     * @param recordsId 访问记录ID
     * @return 结果
     */
    @Override
    public int deleteBieRecordsById(String recordsId)
    {
        return bieRecordsMapper.deleteBieRecordsById(recordsId);
    }


}
